package com.yuxi.Util;  
    
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;   
import java.util.ArrayList; 
import java.util.List;   
import java.util.Map;

import com.yuxi.excelEntity.OwnWordInfo;

import freemarker.template.Configuration;   
import freemarker.template.Template;   
import freemarker.template.TemplateException;   
    
public class ExportOwnWord {   
    private Configuration configuration = null; 
    private String outFilePath;
    private String realPath;
    private String realPathHTML;
    public File temUrl;
    public boolean temFlase=false;
    public String docName;
    public String htmlName;
    
    public ExportOwnWord(String outFilePath,String name) {   
        configuration = new Configuration();   
        this.configuration.setDefaultEncoding("UTF-8");  
        this.outFilePath=outFilePath;
        this.realPath=name+".doc";
        this.realPathHTML=name+".html";
    }   
    
    public void createDoc(Map dataMap) { 
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，   
        //这里我们的模板是放在com.havenliu.document.template包下面       
    	
    	if(temFlase){
    		try {
				configuration.setDirectoryForTemplateLoading(temUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}else{
    		configuration.setClassForTemplateLoading(this.getClass(), "/com/yuxi/tem"); 
    	}
        Template t=null;   
        try {   
            //test.ftl为要装载的模板     
        	if(temFlase){
        		 t = configuration.getTemplate(docName,"UTF-8");  
        		 t.setEncoding("UTF-8");
        	}else{
        		t = configuration.getTemplate("ownTemNew.xml","UTF-8");  
        		 t.setEncoding("UTF-8");
        	}
            
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        //输出文档路径及名称   
        File outFile1 = new File(this.outFilePath);
        
        File outFile = new File(this.outFilePath+this.realPath); 
        try{
        	if(!outFile1 .exists() && !outFile1 .isDirectory())     
        	{ 
        		outFile1 .mkdir();    
        	}
        }catch(Exception e){
        	outFile1 .mkdir(); 
        }
        Writer out = null;   
        try {   
            try {
            	FileOutputStream outs=new FileOutputStream(outFile);
				out = new BufferedWriter(new OutputStreamWriter(outs,"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}   
        } catch (FileNotFoundException e1) {   
            e1.printStackTrace();   
        }   
    
        try { 
        	t.setOutputEncoding("UTF-8");
            t.process(dataMap, out);   
        } catch (TemplateException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }  
    
    
    
    /**  
     * 注意dataMap里存放的数据Key值要与模板中的参数相对应  
     * @param dataMap  
     */  
     public  Map getData(Map dataMap)   
      {   
          dataMap.put("userName", "张三");
          dataMap.put("time", "张三");
          dataMap.put("goodDo", "这是测试备注信息");
          dataMap.put("owntotal", "这是测试备注信息"); 
          dataMap.put("report", "这是测试备注信息"); 
          dataMap.put("suggest", "这是测试备注信息"); 
          dataMap.put("commment", "这是测试备注信息"); 
          
          List 	 _table1=new ArrayList(); 
         for(int i=0;i<10;i++){
        	  OwnWordInfo t2=new OwnWordInfo();        
        	  t2.setInfoconten( "信息内容"+i);
        	  t2.setPersons("23232");
        	  t2.setStanand( "标准");
        	//  t2.setMesure( "措施");
        	  t2.setTask("223");
        	  t2.setTime("1");
        	  t2.setTasktime("23232");
        	  t2.setTaskNames("tasname");
              _table1.add(t2); 
         }         
    
          dataMap.put("table1", _table1); 
          dataMap.put("table2", _table1);
    
        return dataMap;
      }

	
	
    public void createDoc(Map dataMap, ByteArrayOutputStream os) { 
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，   
        //这里我们的模板是放在com.havenliu.document.template包下面   
        configuration.setClassForTemplateLoading(this.getClass(), "/com/yuxi/tem");   
        Template t=null;   
        try {   
            //test.ftl为要装载的模板   
            t = configuration.getTemplate("ownTemNew.xml");
            t.setEncoding("UTF-8");
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        //输出文档路径及名称          
        Writer out = null;
        try {
			out = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}     
        try { 
        	t.setOutputEncoding("UTF-8");
            t.process(dataMap, out);   
        } catch (TemplateException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    } 
    
    public void createHTML(Map dataMap) { 
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，   
        //这里我们的模板是放在com.havenliu.document.template包下面       	
    	if(temFlase){
    		try {
				configuration.setDirectoryForTemplateLoading(temUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}else{
    		configuration.setClassForTemplateLoading(this.getClass(), "/com/yuxi/tem"); 
    	}   
        Template t=null;   
        try {   
            //test.ftl为要装载的模板   
           if(temFlase){
        	   t = configuration.getTemplate(htmlName); 
        	   t.setEncoding("UTF-8");
           }else{
        	   t = configuration.getTemplate("ownTemNew.htm"); 
        	   t.setEncoding("UTF-8");
           }
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        //输出文档路径及名称   
        File outFile1 = new File(this.outFilePath); 
        File outFile = new File(this.outFilePath+this.realPathHTML); 
        try{
        	if(!outFile1 .exists() && !outFile1 .isDirectory())     
        	{ 
        		outFile1 .mkdir();    
        	}
        }catch(Exception e){
        	outFile1 .mkdir(); 
        }
        Writer out = null;   
        try {   
            try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}   
        } catch (FileNotFoundException e1) {   
            e1.printStackTrace();   
        }   
    
        try {  
        	t.setOutputEncoding("UTF-8");
            t.process(dataMap, out);   
        } catch (TemplateException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }  
}   